﻿///////////////////////////////////////////////////////////////////////////////
// File:        Reactor.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef NIO_NET_REACTOR_HPP_
#define NIO_NET_REACTOR_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <nio/Types.hpp>
#include <nio/net/EventHandler.hpp>

namespace nio
{
    namespace net
    {
        class Reactor : public boost::noncopyable
        {
        public:
            enum Event
            {
                RE_READ = 0x1,
                RE_WRITE = 0x2,
                RE_ERROR = 0x4,
                RE_RDERR = RE_READ | RE_ERROR,
                RE_WRERR = RE_WRITE | RE_ERROR,
                RE_RDWR = RE_READ | RE_WRITE,
                RE_ALL = RE_READ | RE_WRITE | RE_ERROR
            };
#if defined(__LINUX__)
            enum Mode
            {
                MD_LT = 1,
                MD_ET
            };
#endif
        public:
            Reactor(void)
                : cnfHeartbeatTime_(60000)
            {

            }

            virtual ~Reactor(void) {}

        public:
            virtual bool addSource(sock_t sock, Event events, EventHandler* handler) = 0;
            virtual void removeSource(sock_t sock) = 0;
            virtual void doTick(base::time_t timeout = 0) = 0;

            void setHeartbeatTime(base::time_t time) { cnfHeartbeatTime_ = time; }

#if defined(__LINUX__)
            virtual void setMode(Mode mode) = 0;
#endif

        protected:
            base::time_t cnfHeartbeatTime_;
        };

        typedef boost::shared_ptr<Reactor> ReactorPtr;
    }
}

#endif //NIO_NET_REACTOR_HPP_
